package com.fenglingzff.reportstudent.controller;


import com.fenglingzff.reportstudent.common.Result;
import com.fenglingzff.reportstudent.entity.Speciality;
import com.fenglingzff.reportstudent.service.SpecialityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zzp
 * @since 2024-08-29
 */
@RestController
@RequestMapping("/speciality")
public class SpecialityController {
    @Autowired
    private SpecialityService specialityService;

    @GetMapping("/getSpecialityCostMap")
    public Result getSpecialityCostMap(){
        List<Speciality> specialityList =specialityService.list();
        Map<String,String> map =specialityList.stream().collect(Collectors.toMap(Speciality::getSpecialtyCode,Speciality::getCost));
        return Result.success(map);
    }

    @GetMapping("/getSpecialityInstituteMap")
    public Result getSpecialityInstituteMap() {
        List<Speciality> specialityList = specialityService.list();
        // 假设specialityList已经填充了数据

        Map<String, String> collegeMap = new HashMap<>();
        for (Speciality speciality : specialityList) {
            String collegeCode = speciality.getInstituteCode();
            if (!collegeMap.containsKey(collegeCode)) {
                collegeMap.put(collegeCode, speciality.getInstitute());
            }
        }
        return Result.success(collegeMap);
    }

    @GetMapping("/getSpecialityList")
    public Result getSpecialityList(){
        return Result.success(specialityService.list());
    }

    @GetMapping("/get")
    public Result getSpecialitybyId(String instituteCode){
        List<Speciality> list;
        if("7".equals(instituteCode)){
             list = specialityService.list();
        }else{
             list = specialityService.lambdaQuery().eq(Speciality::getInstituteCode, instituteCode).list();
        }

        return list.size()>0?Result.success(list):Result.fail();
    }

}
